package com.szcinda.fitness;

import com.szcinda.fitness.persistence.Trainee;
import com.szcinda.fitness.utils.EncodeDecodeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TraineeServiceImpl implements TraineeService {

    private final TraineeRepository traineeRepository;

    public TraineeServiceImpl(TraineeRepository traineeRepository) {
        this.traineeRepository = traineeRepository;
    }

    @Override
    public void check(Trainee trainee) {
        Trainee existTrainee = traineeRepository.findByOpenId(trainee.getOpenId());
        if (existTrainee != null) {
            BeanUtils.copyProperties(trainee, existTrainee);
            existTrainee.setNickName(EncodeDecodeUtil.encode(existTrainee.getNickName()));
            traineeRepository.save(existTrainee);
        } else {
            trainee.setNickName(EncodeDecodeUtil.encode(trainee.getNickName()));
            traineeRepository.save(trainee);
        }
    }
}
